package com.yeepay.bc.app.ut;

import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.LinkedHashMap;
import java.util.Map;
import com.yeepay.bc.app.bp.CreateCashierRequest;
import com.yeepay.g3.sdk.yop.client.YopRequest;
import com.yeepay.g3.sdk.yop.client.YopResponse;
import com.yeepay.g3.sdk.yop.client.YopRsaClient;
import com.yeepay.g3.sdk.yop.encrypt.CertTypeEnum;
import com.yeepay.g3.sdk.yop.encrypt.DigitalEnvelopeDTO;
import com.yeepay.g3.sdk.yop.utils.DigitalEnvelopeUtils;
import com.yeepay.g3.sdk.yop.utils.InternalConfig;

public class CashierPayUtil {
	private CashierPayUtil(){}
	
	@SuppressWarnings("unused")
    public static void main(String s[]){
		CreateCashierRequest request = new CreateCashierRequest();
	}
	
	/**
	 * 创建
	 * @param request
	 * @return
	 */
	@SuppressWarnings("rawtypes")
    public static String createCashier(CreateCashierRequest request){
    	YopRequest yopRequest = new YopRequest();
    	yopRequest.addParam("customerNo",request.getCustomerNo());
    	yopRequest.addParam("amount",request.getAmount());
    	yopRequest.addParam("goodsName",request.getGoodsName());
    	yopRequest.addParam("customerRequestNo",request.getCustomerRequestNo());
    	yopRequest.addParam("frontUrl",request.getFrontUrl());
    	yopRequest.addParam("receiverCallbackUrl",request.getReceiverCallbackUrl());
    	yopRequest.addParam("accountWay",request.getAccountWay());
    	yopRequest.addParam("accountType",request.getAccountType());
    	yopRequest.addParam("directPayType", request.getDirectPayType());
    	//request.addParam("assurePeriod","1");//担保有效期

    	//request.addParam("splitInfo","[{\"amount\":\"0.02\",\"callbackUrl\":\"www.yeepay.com\","
    	//		+ "\"customerNo\":\"10027760998\",\"customerRequestNo\":\"582561210747465729\"}]");
    	
    	//参数 end
		try {
			YopResponse response = YopRsaClient.post("/rest/v1.0/bc/cashier/create-yop-cashier-url", yopRequest);
			handlerResponse(response);
			return ((Map)response.getResult()).get("cashierUrl").toString();
		} catch (Exception e) {
			return null;
		}
	}
	
	public static String handlerNotify(NotifyRequestDTO notify){
		DigitalEnvelopeDTO dto = new DigitalEnvelopeDTO();
		dto.setCipherText(notify.getResponse());
		PrivateKey privateKey = InternalConfig.getISVPrivateKey(CertTypeEnum.RSA2048);
		PublicKey publicKey = InternalConfig.getYopPublicKey(CertTypeEnum.RSA2048);
		dto = DigitalEnvelopeUtils.decrypt(dto, privateKey, publicKey);
		return dto.getPlainText();
	}
	
	@SuppressWarnings("unchecked")
    public static void handlerResponse(YopResponse response) throws Exception{
    	if(response == null || "ERROR".equals(response.getState())){
    		throw new Exception(response == null ? "response is null" : response.toString());
    	}
    	try{
        	LinkedHashMap<String, Object> result = (LinkedHashMap<String, Object>)response.getResult();
            if(!"0000".equals(result.get("retCode"))){
            	throw new Exception();
            }
        }catch (Exception e) {
        	System.err.println(response.toString());
			throw new Exception(response.getStringResult());
		}
    }
}